/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is
*   Nick Kreeger
* Portions created by the Initial Developer are Copyright (C) 2006-2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Nick Kreeger <nick.kreeger@park.edu>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */

#import "MessageViewContainer.h"

#import "MessageHeaderView.h"
#import "AttachmentView.h"
#import "KeychainService.h"

#import "ContentClickListener.h"
#include "nsIDOMWindow.h"
#include "nsIWebBrowser.h"
#include "nsIWebNavigation.h"
#include "nsIWebBrowserSetup.h"
#include "nsIDOMDocument.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIChromeEventHandler.h"
#include "nsPIDOMWindow.h"
#include "nsIDOMEventReceiver.h"
#include "nsIWebProgressListener.h"
#include "nsIBrowserDOMWindow.h"

@interface MessageViewContainer(Private)

-(void)ensureContentClickListeners;

@end

@implementation MessageViewContainer

-(id)initWithFrame:(NSRect)aFrame
{
  if ((self = [super initWithFrame:aFrame]))
  {
    mIsHeaderViewVisible = NO;
    mIsAttachmentViewVisible = NO;
    mShouldInsertHeaderView = NO;
    mShouldInsertAttachmentView = NO;
  }
  
  return self;
}

-(void)windowWillClose
{
  [mBrowserView removeListener:self];
  [mBrowserView destroyWebBrowser];
}

-(void)showMessageHeaderView
{
  mIsHeaderViewVisible = YES;
  mShouldInsertHeaderView = YES;
  [self display];
}

-(void)removeMessageHeaderView
{
  mIsHeaderViewVisible = NO;
  [mMessageHeaderView removeFromSuperview];
  [self display];
}

-(void)updateViewForDummyHeader:(DummyMessageHeader*)aDummyMessageHeader
{
  [mMessageHeaderView updateViewForDummyHeader:aDummyMessageHeader];
}

-(void)clearMessageView
{
  mIsHeaderViewVisible = NO;
  mIsAttachmentViewVisible = NO;
  [mMessageHeaderView clearValues];
  [mMessageHeaderView removeFromSuperview];
  [mAttachmentView removeFromSuperview];
  [self display];
}

-(void)showAttachmentView:(NSArray*)inAttachmentArr
{
  mIsAttachmentViewVisible = YES;
  [mAttachmentView updateAttachmentList:inAttachmentArr];
  mShouldInsertAttachmentView = YES;
  [self addSubview:(NSView*)mAttachmentView];
  [self display];
}

-(void)removeAttachmentView
{
  if (!mIsAttachmentViewVisible)
    return;
  
  mIsAttachmentViewVisible = NO;
  [mAttachmentView removeFromSuperview];
  [self display];
}

-(void)setBrowserViewActive:(BOOL)aActiveState
{
  [mBrowserView setActive:aActiveState];
}

-(void)setBrowserView:(CHBrowserView*)aBrowserView
{
  mBrowserView = aBrowserView;
  [self addSubview:mBrowserView];
  [self setAutoresizesSubviews:YES];
  [self ensureContentClickListeners];
  [[KeychainService instance] addListenerToView:mBrowserView];
}

-(void)setFrame:(NSRect)frameRect
{
  [super setFrame:frameRect];
  //[self display];  // hack.
}

-(void)display
{
  if (mIsHeaderViewVisible)
  {
    NSRect bounds = [self bounds];
    NSRect headerFrame = [mMessageHeaderView frame];
    NSRect browserFrame = [mBrowserView frame];
    
    float divideCoord = bounds.size.height - headerFrame.size.height;
    browserFrame.origin = NSMakePoint(1, 1);  // offset by one for border
    
    // Insert the message header view if needed.
    if (mShouldInsertHeaderView)
    {
      [self addSubview:(NSView*)mMessageHeaderView];
      mShouldInsertHeaderView = NO;
    }
    
    // Add the attachment view to the bottom of the frame
    if (mIsAttachmentViewVisible)
    {
      if (mShouldInsertAttachmentView)
      {
        [self addSubview:(NSView*)mAttachmentView];
        mShouldInsertAttachmentView = NO;
      }
      
      NSRect attachFrame = [mAttachmentView frame];
      divideCoord -= attachFrame.size.height;
      attachFrame.size.width = bounds.size.width;
      browserFrame.origin.y = attachFrame.size.height;
      browserFrame.size.height = divideCoord;
      [mAttachmentView setFrame:attachFrame];
    }
    else
      browserFrame.size.height = divideCoord - 1; 

    browserFrame.size.width = bounds.size.width - 1;
    
    headerFrame.origin.y = browserFrame.size.height + browserFrame.origin.y;
    headerFrame.size.width = bounds.size.width;
    
    [mMessageHeaderView setFrame:headerFrame];
    [mBrowserView setFrame:browserFrame];
  }
  else
  {
    // Remove 1px around web view for border
    NSRect bounds = [self bounds];
    bounds.size.width = bounds.size.width - 1;
    bounds.size.height = bounds.size.height - 2;
    bounds.origin = NSMakePoint(1, 1);
    [mBrowserView setFrame:bounds];
  }
  
  [super display];
}

-(void)drawRect:(NSRect)aRect
{
  [super drawRect:aRect];
  
  NSRect frame = [self bounds];
  
  [[NSColor lightGrayColor] set];
  NSRectFill(frame);
}

-(void)resizeSubviewsWithOldSize:(NSSize)oldFrameSize
{
  [self display];
}

-(void)ensureContentClickListeners
{ 
    // We need to hook up our click and context menu listeners.
  ContentClickListener* clickListener = new ContentClickListener([[self window] delegate]);
  if (!clickListener)
    return;
  
  nsCOMPtr<nsIDOMWindow> contentWindow = [mBrowserView getContentWindow];
  nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(contentWindow));
  nsIChromeEventHandler *chromeHandler = piWindow->GetChromeEventHandler();
  nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(chromeHandler));
  if ( rec )
    rec->AddEventListenerByIID(clickListener, NS_GET_IID(nsIDOMMouseListener));
}

#pragma mark -

-(void)setStatus:(NSString*)statusString ofType:(NSStatusType)type
{
}

-(NSString*)title
{
  return @"";
}

-(void)setTitle:(NSString*)title
{
}

-(void)sizeBrowserTo:(NSSize)dimensions
{
}

-(CHBrowserView*)createBrowserWindow:(unsigned int)mask
{
  NSLog(@"createBrowserWindow:");
  return nil;
}

-(CHBrowserView*)reuseExistingBrowserWindow:(unsigned int)mask
{
  NSLog(@"reuseExistingBrowserWindow:");
  return nil;
}

-(BOOL)shouldReuseExistingWindow
{
  return YES;
}

-(int)respectWindowOpenCallsWithSizeAndPosition
{
  return 0;
}
  
-(NSMenu*)getContextMenu
{
  return nil;
}

-(NSWindow*)getNativeWindow
{
  return [self window];
}

-(BOOL)shouldAcceptDragFromSource:(id)dragSource
{
  return NO;
}

-(void)closeBrowserWindow
{
  NSLog(@"closeBrowserWindow");
}

-(void)willShowPrompt
{
  //NSLog(@"willShowPrompt:");
}

-(void)didDismissPrompt
{
  //NSLog(@"willDismissPrompt:");
}

@end
